package com.jwell.panprovider.ui.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Rect
import android.view.*
import com.jwell.panprovider.R
import com.jwell.panprovider.model.PreRegisterModel
import com.jwell.panprovider.model.ProviderRegisterModel
import kotlinx.android.synthetic.main.dialog_pre_register_layout.view.*

/**
 * Created by 76577 on 2018/4/9.
 * 预登记弹出框
 */
class PreRegisterDialog {
    class Builder(private val mContext: Context, var data: ProviderRegisterModel.RequestData) {

        private var listener: SusreOnclickListener? = null

        fun setSureOnclickListener(listener: SusreOnclickListener): Builder {
            this.listener = listener
            return this
        }

        fun create(): Dialog {
            val dialog = Dialog(mContext)
            val convertView = LayoutInflater.from(mContext).inflate(R.layout.dialog_pre_register_layout, null)
            with(convertView) {
                text_carNum.text = data.carNum ?: ""
                text_driverName.text = data.driverName ?: ""
                text_driverPhone.text = data.driverPhone ?: ""
                text_remark.text = data.remark ?: ""
                when (data.measurementMethod) {
                    3 -> {
                        ll_measurementMethod.visibility = View.VISIBLE
                        text_measurementMethod.text = "标证除皮"
                    }
                    1 -> {
                        ll_measurementMethod.visibility = View.VISIBLE
                        text_measurementMethod.text = "先重后空"
                    }
                    else -> {
                        ll_measurementMethod.visibility = View.GONE
                    }
                }
                ll_btn1.setOnClickListener {
                    dialog.dismiss()
                }
                ll_btn2.setOnClickListener {
                    dialog.dismiss()
                    listener?.onClick()
                }
            }
            //其他设置
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE) //设置没有标题
            val window = dialog.window
            dialog.setCanceledOnTouchOutside(true)//设置Dialog以外是否可触摸
            window.setGravity(Gravity.BOTTOM)  //此处可以设置dialog显示的位置
            window.setWindowAnimations(R.style.mystyle)  //添加动画
            window.setContentView(convertView)
            val windowparams = dialog.window.attributes
            val rect = Rect()
            val view1 = dialog.window.decorView
            view1.getWindowVisibleDisplayFrame(rect)
            windowparams.width = WindowManager.LayoutParams.MATCH_PARENT
            dialog.window.setBackgroundDrawableResource(android.R.color.transparent)
            dialog.window.attributes = windowparams as android.view.WindowManager.LayoutParams
            return dialog
        }
    }

    interface SusreOnclickListener {
        fun onClick()
    }
}